package com.china_goldcard.myactionsheet;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jk on 16/10/24.
 */
public class MyActionSheet extends Dialog {

    private RecyclerView recyclerView;

    private TextView cancelBtn;

    private List<String> titles = new ArrayList<>();

    private String cancelTitle = "取消";

    private float itemH = 44;

    private RelativeLayout rootView;

    private boolean canCancelOutside = true;

    private float itemPadding = 5;

    private MyActionSheetAdapter actionSheetAdapter;

    public void setOnBtnItemClickListener(OnBtnItemClickListener onBtnItemClickListener) {
        this.onBtnItemClickListener = onBtnItemClickListener;
    }

    private OnBtnItemClickListener onBtnItemClickListener;

    public MyActionSheet(Context context) {
          this(context,true);
    }

    public MyActionSheet(Context context,boolean canCancelOutside){
        super(context,R.style.MyActionSheet);
        this.canCancelOutside = canCancelOutside;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(canCancelOutside);
        setCancelable(canCancelOutside);
        getWindow().setGravity(Gravity.BOTTOM);
        initViews();
        initEvents();
        updateUI();
    }

    public void setItemH(float itemH) {
        this.itemH = itemH;
    }


    /**
     * 更新UI
     */
    private void updateUI(){
        cancelBtn.setText(trimNull(cancelTitle,"取消"));
        cancelBtn.getLayoutParams().height = dip2px(getContext(),itemH);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(getContext(),LinearLayoutManager.VERTICAL,2,R.color.bg_color));
        actionSheetAdapter = new MyActionSheetAdapter(getContext(),titles,R.layout.my_action_sheet_item);
        actionSheetAdapter.setItemH(dip2px(getContext(),itemH));
        actionSheetAdapter.setOnItemClickListener(new MyActionSheetAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(String item, int position) {
               if(onBtnItemClickListener!=null){
                   onBtnItemClickListener.onBtnItemClicked(MyActionSheet.this,item,position);
               }
            }
        });
        recyclerView.setAdapter(actionSheetAdapter);
        int rootH = dip2px(getContext(),(itemH * (titles.size() + 1) + itemPadding));
        rootView.getLayoutParams().height = rootH;
        rootView.getLayoutParams().width = screenW(getContext());
    }

    /**
     * trimNull方法
     * @param str
     * @param defaultStr
     * @return
     */
    private String trimNull(String str,String defaultStr){
        if (str == null || str.trim().isEmpty()){
            return defaultStr;
        }else{
            return str.trim();
        }
    }

    /**
     * 初始化视图
     */
    private void initViews(){
        setContentView(R.layout.my_action_sheet_view);
        recyclerView = (RecyclerView) findViewById(R.id.my_action_sheet_btn);
        cancelBtn = (TextView) findViewById(R.id.my_action_sheet_cancal_btn);
        rootView = (RelativeLayout) findViewById(R.id.my_action_sheet_root);
    }

    public String getCancelTitle() {
        return cancelTitle;
    }

    public void setCancelTitle(String cancelTitle) {
        this.cancelTitle = cancelTitle;
    }

    public List<String> getTitles() {
        return titles;
    }

    public void setTitles(List<String> titles) {
        this.titles = titles;
    }

    /**
     * 初始化事件监听
     */
    private void initEvents(){
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onBtnItemClickListener!=null){
                    onBtnItemClickListener.onCancelBtnClicked(v,MyActionSheet.this);
                }
            }
        });
    }

    public interface OnBtnItemClickListener{
        void onCancelBtnClicked(View v,MyActionSheet actionSheet);
        void onBtnItemClicked(MyActionSheet actionSheet,String title,int position);
    }

    /**
     * Builder for arguments
     */
    public static class Builder {
        private Context context;
        private List<String> titles = new ArrayList<>();
        private String cancelTxt;
        private OnBtnItemClickListener onBtnItemClickListener;
        private boolean canCancelOutside = true;
        private float itemH = 44;

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setCanCancelOutside(boolean canCancelOutside){
            this.canCancelOutside = canCancelOutside;
            return this;
        }

        public Builder setTitles(String ... titles1) {
            for(String title : titles1){
                titles.add(title);
            }
            return this;
        }

        public Builder setItemH(float itemH){
            this.itemH = itemH;
            return this;
        }

        public Builder setCancelText(String cancelTxt) {
            this.cancelTxt = cancelTxt;
            return this;
        }

        public Builder setOnBtnClickListener(OnBtnItemClickListener onBtnItemClickListener) {
            this.onBtnItemClickListener = onBtnItemClickListener;
            return this;
        }

        public MyActionSheet build() {
            MyActionSheet dialog = new MyActionSheet(context,canCancelOutside);
            dialog.setTitles(titles);
            dialog.setCancelTitle(cancelTxt);
            dialog.setItemH(itemH);
            dialog.setOnBtnItemClickListener(onBtnItemClickListener);
            return dialog;
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     * @param context
     * @param dpValue 要转换的dp值
     */
    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     * @param context
     * @param pxValue 要转换的px值
     */
    private int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 获得屏幕宽度
     * @param context
     * @return
     */
    public int screenW(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

}
